# -*- coding: utf-8 -*-

import json
import tkinter as tk
import tkinter.messagebox
import pyperclip

from data.data_out import get_source, save_data_labeled, update_source


class MAIN(object):

    def __init__(self, master, **kwargs):
        global data_str, country_str, province_str, grape_str, price_str
        global country_entry, province_entry, grape_entry, price_entry
        global country_tag, province_tag, grape_tag, price_tag
        data_str= tk.StringVar()

        country_tag = tk.StringVar()
        province_tag = tk.StringVar()
        grape_tag = tk.StringVar()
        price_tag = tk.StringVar()

        country_str = tk.StringVar()
        province_str = tk.StringVar()
        grape_str = tk.StringVar()
        price_str = tk.StringVar()
        data_str.set(json.dumps(data_json[0]))

        country_tag.set(data_json[0]['country'])
        province_tag.set(data_json[0]['province'])
        grape_tag.set(data_json[0]['variety'])
        price_tag.set(data_json[0]['price'])

        country_str.set('inform:country = ' + data_json[0]['country'])
        province_str.set('inform:province = ' + data_json[0]['province'])
        grape_str.set('inform:variety = ' + data_json[0]['variety'])
        price_str.set('inform:price = ' + data_json[0]['price'])

        # initialize window frame
        window = tk.Frame(master)
        window.grid()

        row_num = 0
        # add a label for instruction
        instruction_label = tk.Label(window,
                                     text=instruction_str,
                                     bg='yellow',     # 背景颜色
                                     font=('Arial', 15),  # 字体和字体大小
                                     # width=16, height=2  # 标签长宽
                                     ).grid(row=row_num, sticky='w')
        row_num = row_num + 1
        agent_label1 = tk.Label(window,
                                text='Which country are you in?',
                                font=('Arial', 15),  # 字体
                                ).grid(row=row_num, column = 1, sticky='e')
        row_num = row_num + 1

        inform_label1 = tk.Label(window,
                                 textvariable=country_str,
                                 font=('Arial', 15),  # 字体
                                 #wraplength=600,
                                 #anchor='w',
                                 #justify='left'
                                 ).grid(row=row_num, sticky='w')

        tag_label1 = tk.Label(window,
                                 text='Tag:',
                                 font=('Arial', 15),  # 字体
                                 ).grid(row=row_num, column = 1,sticky='w')

        country_tag_entry = tk.Entry(window, width = 21, font = 14, textvariable=country_tag)
        country_tag_entry.grid(row=row_num, column=0,columnspan = 2,  sticky='e')
        row_num = row_num + 1

        user_label1 = tk.Label(window,
                              text='User Write:',
                              font=('Arial', 15),  # 字体
                              ).grid(row=row_num, sticky='w')

        country_entry = tk.Entry(window, width = 47, font = 14)
        country_entry.bind('<FocusIn>', lambda event:self.set_text(country_tag.get()))
        country_entry.grid(row=row_num, column=0,columnspan = 2,sticky = 'e')
        row_num = row_num + 1

        agent_label2 = tk.Label(window,
                                text='which province are you in?',  # 标签的文字
                                font=('Arial', 15),  # 字
                                #wraplength=600,
                                #anchor='e',
                                #justify='right'
                                ).grid(row=row_num, column = 1, sticky='e')
        row_num = row_num + 1

        inform_label2 = tk.Label(window,
                                 textvariable=province_str,
                                 font=('Arial', 15),  # 字体
                                 #wraplength=600,
                                 #anchor='w',
                                 #justify='left'
                                 ).grid(row=row_num, sticky='w')

        tag_label1 = tk.Label(window,
                              text='Tag:',
                              font=('Arial', 15),  # 字体
                              ).grid(row=row_num, column=1, sticky='w')

        province_tag_entry = tk.Entry(window, width = 21, font = 14, textvariable=province_tag)
        province_tag_entry.grid(row=row_num, column=0, columnspan=2, sticky='e')
        row_num = row_num + 1

        user_label1 = tk.Label(window,
                               text='User Write:',
                               font=('Arial', 15),  # 字体
                               ).grid(row=row_num, sticky='w')

        province_entry = tk.Entry(window, width = 47, font = 14)
        province_entry.bind('<FocusIn>', lambda event:self.set_text(province_tag.get()))
        province_entry.grid(row=row_num, column=0, columnspan=2, sticky='e')
        row_num = row_num + 1

        agent_label3 = tk.Label(window,
                                text='Which grape do you like?',  # 标签的文字
                                font=('Arial', 15),  # 字
                                #wraplength=600,
                                #anchor='e',
                                #justify='right'
                                ).grid(row=row_num,column = 1, sticky='e')
        row_num = row_num + 1

        inform_label2 = tk.Label(window,
                                 textvariable=grape_str,
                                 font=('Arial', 15),  # 字体
                                 #wraplength=600,
                                 #anchor='w',
                                 #justify='left'
                                 ).grid(row=row_num, sticky='w')

        tag_label1 = tk.Label(window,
                              text='Tag:',
                              font=('Arial', 15),  # 字体
                              ).grid(row=row_num, column=1, sticky='w')

        grape_tag_entry = tk.Entry(window, width = 21, font = 14, textvariable=grape_tag)
        grape_tag_entry.grid(row=row_num, column=0, columnspan=2, sticky='e')
        row_num = row_num + 1

        user_label1 = tk.Label(window,
                               text='User Write:',
                               font=('Arial', 15),  # 字体
                               ).grid(row=row_num, sticky='w')

        grape_entry = tk.Entry(window, width = 47, font = 14)
        grape_entry.bind('<FocusIn>', lambda event:self.set_text(grape_tag.get()))
        grape_entry.grid(row=row_num, column=0, columnspan=2, sticky='e')
        row_num = row_num + 1

        agent_label4 = tk.Label(window,
                                text='What rating range do you want?',  # 标签的文字
                                font=('Arial', 15),  # 字
                                # width=15, height=2  # 标签长宽
                                #wraplength=600,
                                #anchor='e',
                                #justify='right'
                                ).grid(row=row_num, column = 1,sticky='e')
        row_num = row_num + 1

        inform_label3 = tk.Label(window,
                                 textvariable=price_str,
                                 font=('Arial', 15),  # 字体
                                 #wraplength=600,
                                 #anchor='w',
                                 #justify='left'
                                 ).grid(row=row_num, sticky='w')

        tag_label1 = tk.Label(window,
                              text='Tag:',
                              font=('Arial', 15),  # 字体
                              ).grid(row=row_num, column=1, sticky='w')

        price_tag_entry = tk.Entry(window, width = 21, font = 14, textvariable=price_tag)
        price_tag_entry.grid(row=row_num, column=0, columnspan=2, sticky='e')
        row_num = row_num + 1

        user_label1 = tk.Label(window,
                               text='User Write:',
                               font=('Arial', 15),  # 字体
                               ).grid(row=row_num, sticky='w')

        price_entry = tk.Entry(window, width = 47, font = 14)
        price_entry.bind('<FocusIn>', lambda event:self.set_text(price_tag.get()))
        price_entry.grid(row=row_num, column=0, columnspan=2, sticky='e')
        row_num = row_num + 1

        supply_button = tk.Button(window, bg='yellow',text='提交', command = self.refresh_clicked)
        supply_button.grid(row=row_num, columnspan=2)

    def set_text(self, aString):  # 写入剪切板
        # w.OpenClipboard()
        # w.EmptyClipboard()
        # w.SetClipboardData(win32con.CF_TEXT, aString)
        # w.CloseClipboard()
        pyperclip.copy(aString)

    def refresh_clicked(event):
        global data_str, country_str, province_str, grape_str, price_str
        global country_entry, province_entry, grape_entry, price_entry
        global country_tag, province_tag, grape_tag, price_tag

        data_labeled= []
        tag_labeled = []
        country_sentence = country_entry.get()
        province_sentence = province_entry.get()
        grape_sentence = grape_entry.get()
        price_sentence = price_entry.get()
        data_labeled.append(json.dumps(data_json[0]))
        data_labeled.append(country_sentence)
        data_labeled.append(province_sentence)
        data_labeled.append(grape_sentence)
        data_labeled.append(price_sentence)

        if country_tag.get() in country_sentence:
            tag_labeled.append(country_sentence.replace(country_tag.get(), '<country>'))
        else:
            show_me()
            return
        if province_tag.get() in province_sentence:
            tag_labeled.append(province_sentence.replace(province_tag.get(), '<province>'))
        else:
            show_me()
            return
        if grape_tag.get() in grape_sentence:
            tag_labeled.append(grape_sentence.replace(grape_tag.get(), '<grape>'))
        else:
            show_me()
            return
        if price_tag.get() in price_sentence:
            tag_labeled.append(price_sentence.replace(price_tag.get(), '<price>'))
        else:
            show_me()
            return

        # 删除输入框所有输入
        country_entry.delete(0, tk.END)
        province_entry.delete(0, tk.END)
        grape_entry.delete(0, tk.END)
        price_entry.delete(0, tk.END)

        # 保存标注数据
        save_data_labeled(data_labeled)
        save_data_labeled(tag_labeled)

        data_json.pop(0)
        # 更新源数据
        update_source(data_json)

        data_str.set(data_json[0])
        country_str.set('inform:country = ' + data_json[0]['country'])
        province_str.set('inform:province = ' + data_json[0]['province'])
        grape_str.set('inform:variety = ' + data_json[0]['variety'])
        price_str.set('inform:price = ' + data_json[0]['price'])

        country_tag.set(data_json[0]['country'])
        province_tag.set(data_json[0]['province'])
        grape_tag.set(data_json[0]['variety'])
        price_tag.set(data_json[0]['price'])


def show_me():
    tk.messagebox.showinfo(title='Hi', message='请确认您的输入中是否包含TAG')


data_json = get_source()
instruction_str = 'Please try to use different expressions'
# root = tk.Tk()
# app = MAIN(root)
# root.mainloop()